var objectID='';
var Material = Parse.Object.extend(TABLE_PROJECTMATERIAL);
var Project = Parse.Object.extend(TABLE_PROJECT);
var Step = Parse.Object.extend(TABLE_PROJECTSTEP);
var Tool = Parse.Object.extend(TABLE_PROJECTTOOL);
var Contractor = Parse.Object.extend(TABLE_PROJECTCONTRACTOR);
var UserProject = Parse.Object.extend(TABLE_USERPROJECT);
var idList = new Array();
var statusList = new Array();
var shouldEdit = new Array();
$(document).ready(function(){
    if(!_currentUser){
        window.location = baseurl;
    }
    setActive2ndMenu(1);
   var userQuery = new Parse.Query(UserProject);
   var query = new Parse.Query(Project);
   var projectid = new Array();
   userQuery.equalTo('user',_currentUser);
   if(status !=0){
    userQuery.equalTo('type',Number(status));
   }
   	userQuery.find({
	  success: function(results) {	  
       	   		  for(i=0; i< results.length; i++){
		      projectid[i] = results[i].get('projectId').id;
              idList[i] = results[i].id;
              statusList[i]=results[i].get('type');
		  }
          	var prolist;
    	   prolist= '';
            query.containedIn('objectId',projectid);
        	query.find({
        	  success: function(results) {
        	   if(results.length>0){
        	       $('#nodata-project-list').hide();
                   displayMyProject(results);
        	   }                  
                  else{
                     displayNoData('#project-list','No data found');
                  }
        	  },
        	  error: function(error) {
                displayNoData('#project-list','No data found');
        	  }
        	});
	  },
	  error: function(error) {
        displayNoData('#project-list','No data found');
        $('').html('You haven\'t got any project yet');
        $('#nodata-project-list').show();
	  }
	});
   
});

function displayMyProject(results){
	var prolist ='';
            for(var i=0; i<results.length; i++){
                edit = '';
                
                if(results[i].get('user')&&results[i].get('user').id==_currentUser.id){
                    edit =  '<a class="myproject-item" href="'+linkEditProject(results[i].id)+'">'+
                    '<span class="icon-edit"></span></a>';
                }                              
                 else edit = '';
        prolist = '<div class= "grid" id = "project_no_'+idList[i]  +'">'+
                     '<div class="img"><a href="#"><img src="images/data/image1.jpg" alt="image"></a></div>'+
                     '<div class="g-content">'+
                            '<div class="g-row"><a class="g-title" href="'+linkProject(results[i].id)+'">'
                                +stripTags(results[i].get("title"))+
                            '</a></div>'+
                            '<div class="g-row"><label>Budget:</label> '+stripTags(results[i].get("cost"))+'$</div>'+
                            '<div class="g-row"><label>Status:</label><span id="proStatus_'+idList[i]+'">Todo</span></div>'+
                            '<div class="g-row"><label>Time span:</label> '+results[i].get("time")+'</div>'+
                            '<div class="g-row">'+
                                '<div class="star-outer">'+
                                    '<div class="star-outer" id="rate_project_'+results[i].id+'"></div>'+
                                '</div>'+
                            '</div>'+
                    '</div>'+
                    '<div class="desc">'+
                        '<span class="desc-title">Descriptions:&nbsp;</span>'+stripTags(results[i].get("description"))+
                    '</div>'+
                    '<div class="myproject-button">'+
                         edit +  
                        '<a class="myproject-item" onclick="deleteProject(\''+idList[i]+'\');"><span class="icon-remove"></span></a>'+
                    '</div>'+
                '</div><!--grid-->';
                
                 $("#project-list").append(prolist);
                var currentRate = Number(results[i].get("rateScore")/results[i].get("rateTotal"));
                if(currentRate>5){
                    currentRate = 5;
                }
                var currentRate = Number(results[i].get("rateScore")/results[i].get("rateTotal"));
                if(currentRate>5){
                    currentRate = 5;
                }
                $('#rate_project_'+results[i].id).ratings(5,currentRate,false);
                displayProType(idList[i],statusList[i]);
	  }
}

function deleteProject(objKey){
    var project = new UserProject();
    project.set({id:objKey});
    project.destroy({
      success: function(userProject) {
        // The object was deleted from the Parse Cloud.
         $("#project_no_"+objKey).remove();
      },
      error: function(myObject, error) {
        alert('Error: '+error.message);
      }
});
}
function displayProType(projectId,type){
    switch(type){
        case 2:
        $("#proStatus_"+projectId).html(
        //'Change status: '+
         '<a onclick="changeStatus(1,\''+projectId+'\');">Todo</a>  '+
         '<a ><strong>Interested</strong></a> '+
         '<a onclick="changeStatus(3,\''+projectId+'\');">Done</a> ');
        break;
        case 3:
         $("#proStatus_"+projectId).html(
         '<a onclick="changeStatus(1,\''+projectId+'\');">Todo</a> '+
          '<a href="javascript:void(0);"  onclick="changeStatus(2,\''+projectId+'\');">Interested</a> '+
          '<a><strong>Done</strong></a>           '
          ); 
        break;
        default:
        $("#proStatus_"+projectId).html('<a href="javascript:void();"><strong>Todo</strong> </a>           '+
        //'Change status: '+
         '<a href="javascript:void(0);"  onclick="changeStatus(2,\''+projectId+'\');">Interested</a> '+
         '<a href="javascript:void(0);" onclick="changeStatus(3,\''+projectId+'\');">Done</a>');
        break;
    }
}

function changeStatus(type, proid){
     var userProject = new UserProject();
             userProject.set({id:proid});
              userProject.save({
                  type: type
                 },{              
                     success: function (obj){
                        displayProType(proid, type);
                      },
                     error: function(model, error) {
                      alert('Error: '+error.message);
                      return false;
                    }
                    });
}